package com.custom.base.entity

import android.view.View
import com.github.jdsjlzx.recyclerview.LRecyclerView
import com.custom.base.base.BaseRecyclerAdapter
import com.custom.base.http.OkHttpCallback
import com.custom.base.http.OkHttpFromBoy
import com.custom.base.http.OkHttpManager
import com.custom.base.util.SDViewUtil

import com.custom.base.view.RecyclerListView

/**
 * 作者：
 * 日期：2019/8/8
 * 描述：分页model
 */
class PageModel {
    private lateinit var mCallback: OkHttpCallback.Callback
    private var mFormBody: OkHttpFromBoy? = null
    private var isLoadMore: Boolean = false//是否添加到末尾
    private var listView: LRecyclerView? = null
    private var recyclerListView: RecyclerListView? = null
    var page = 1
        private set
    var pageSize = 10
        private set
    var totalPages = -1
        private set

    fun initial() {
        page = 1
    }

    fun nextPage() {
        page++
    }
    fun rollbackPage() {
        page--
    }

    fun <T> initPullToRefreshListView(
        recyclerListView: RecyclerListView?,
        adapter: BaseRecyclerAdapter<T>?,
        header: View? = null//头部
    ) {
        this.recyclerListView = recyclerListView
        recyclerListView?.apply {
            this@PageModel.listView = lr
            initPullToRefreshListView(adapter,object :
                RecyclerListView.OnPullRefreshListener{
                override fun onPullDownToRefresh(recyclerListView: RecyclerListView) {
                    initial()
                    if(mFormBody!=null){
                        isLoadMore = false
                        request(mFormBody, mCallback)
                    }
                }

                override fun onPullUpToRefresh(recyclerListView: RecyclerListView) {
                    nextPage()
                    if(mFormBody!=null){
                        isLoadMore = true
                        request(mFormBody, mCallback)
                    }
                }
            },header)
        }
    }

    fun request(
        formBody: OkHttpFromBoy?,
        callback: OkHttpCallback.Callback
    ){
        this.mFormBody = formBody
        this.mCallback = callback
        OkHttpManager.instance.requestInterface(formBody,callback)
    }

    /**
     * 更新adapter数据
     */
    fun <T> updateAdapterByList(
        listOriginalData: MutableList<T>?,
        listNewData: List<T>?,
        mAdapter: BaseRecyclerAdapter<T>? = null,
        totalPages:Int = -1
    ) {
        this.totalPages = totalPages
        SDViewUtil.updateAdapterByList(listOriginalData,listNewData,mAdapter,isLoadMore,listView,recyclerListView?.nData)
        if(this.totalPages!=-1){
            if(page >= this.totalPages){
                listView?.setNoMore(true)
            }
        }
    }
    /**
     * 更新adapter数据
     */
    fun <T> updateAdapterByArrayList(
        listOriginalData: MutableList<T>?,
        listNewData: ArrayList<T>?,
        mAdapter: BaseRecyclerAdapter<T>? = null,
        totalPages:Int = -1
    ) {
        this.totalPages = totalPages
        SDViewUtil.updateAdapterByArrayList(listOriginalData,listNewData,mAdapter,isLoadMore,listView,recyclerListView?.nData)
        if(this.totalPages!=-1){
            if(page >= this.totalPages){
                listView?.setNoMore(true)
            }
        }
    }

    fun onRefreshComplete(){
        listView?.refreshComplete(10)
    }

    /**
     * 从第一页开始刷新
     */
    fun initialRefresh() {
        page = 1
        listView?.refresh()
    }

    /**
     * 设置一页数量
     */
    fun setPageSize(size:Int){
        pageSize = size
    }
}
